package au.edu.unsw.editingtool.global;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class PropertyReader {

    private Hashtable<String, String> attributes;
    private Hashtable<Vector<String>, String> config;
    private String filter;
    private String file;
    private String rootPath;

    public PropertyReader() {
        this.attributes = new Hashtable<String, String>();
        this.config = null;
        this.filter = "";
        this.file = "";
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public void setFile(String path) {
        this.file = path;        
    }
    
    public void setPath(String path) {
        this.rootPath = path;
    }
    
    public String getPath() {
        return this.rootPath;
    }
    
    public Hashtable<String, String> getProperties() {
        return this.attributes;
    }
    
    public void clean() {
    	this.attributes.clear();
    }

    public void retrieveProperties() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(this.file));
        String line;
        String currLine[] = null;
        
        while ((line = br.readLine()) != null) 
        {
            if (line.trim().isEmpty() || (line.trim().charAt(0) == '#'))
                continue;
            currLine = line.split("=", 2);
            if (currLine.length != 2)
                continue;
            if (currLine[0].trim().regionMatches(0, this.filter, 0, this.filter.length()))
                this.attributes.put(currLine[0].trim().substring(this.filter.length()+1), currLine[1].trim());
        }
    }
    
    public void buildConfiguration() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(this.file));
        String line;                  
        Hashtable <Vector<String>,String> data = new Hashtable<Vector<String>,String>();
        
        while ((line = br.readLine()) != null) 
        {
            if (line.trim().isEmpty() || (line.trim().charAt(0) == '#'))
                continue;
            String[] currLine = line.split("=", 2);
            if (currLine.length != 2)
                continue;
            String[] tempheaders = currLine[0].split("\\.");            
                
            Vector<String> headers = new Vector<String>();
            for (int i=0;i<tempheaders.length;i++)
            {
            	if(!(tempheaders[i].trim().isEmpty()))
            	{
            		//System.out.println([PROPERTYREADER] buildConfiguration : "+i+". HEADER = "+tempheaders[i].trim());
            		headers.add(tempheaders[i].trim());
            	}
            }            
            data.put(headers, currLine[1].trim());                        
        }   
        this.config = data;
        //System.out.println("[PROPERTYREADER] buildConfiguration : Config Total Data = "+data.size());
    }
    
    public String getConfigValue(String[] filter)
    {    	    	
    	Enumeration<Vector<String>> keys = this.config.keys();
    	while(keys.hasMoreElements())
    	{
    		Vector<String> headers = keys.nextElement();
    		int counter = 0;
    		if (headers.size() == filter.length)
    		{	
    			//System.out.println("[PROPERTYREADER] getConfigValue : Header Size="+headers.size()+" AND Filter Size="+filter.length);
    			for (int i=0;i<filter.length;i++)
    			{
    				if(headers.elementAt(i).trim().compareToIgnoreCase(filter[i].trim()) == 0)
    				{    					
    					counter++;
        				//System.out.println("[PROPERTYREADER] getConfigValue : Header"+i+"="+headers.elementAt(i)+" Filter"+i+"="+filter[i]);        				    					
    				}
    			}
    		}
    		// All keys match the one in filter
    		if(counter == filter.length)
    		{
    			//System.out.println("[PROPERTYREADER] getConfigValue : RESULT = " + this.config.get(headers));
    			return this.config.get(headers); 		
    		}
    		else
    			counter=0;
    	}    	
    	return null;
    }    
}
